// threeDistanceUtils.js
import * as THREE from 'three'

export class DistanceUtils {
  /**
   * 计算两点之间的直线距离
   */
  static getDistance(pointA, pointB) {
    return pointA.distanceTo(pointB)
  }

  /**
   * 计算两点之间的平方距离（性能更好）
   */
  static getDistanceSquared(pointA, pointB) {
    return pointA.distanceToSquared(pointB)
  }

  /**
   * 检查两点是否在指定距离内
   */
  static isWithinDistance(pointA, pointB, maxDistance) {
    return pointA.distanceTo(pointB) <= maxDistance
  }

  /**
   * 检查两点是否在指定平方距离内（性能更好）
   */
  static isWithinDistanceSquared(pointA, pointB, maxDistanceSquared) {
    return pointA.distanceToSquared(pointB) <= maxDistanceSquared
  }

  /**
   * 计算曼哈顿距离（网格距离）
   */
  static getManhattanDistance(pointA, pointB) {
    return (
      Math.abs(pointB.x - pointA.x) +
      Math.abs(pointB.y - pointA.y) +
      Math.abs(pointB.z - pointA.z)
    )
  }

  /**
   * 计算切比雪夫距离（棋盘距离）
   */
  static getChebyshevDistance(pointA, pointB) {
    return Math.max(
      Math.abs(pointB.x - pointA.x),
      Math.abs(pointB.y - pointA.y),
      Math.abs(pointB.z - pointA.z)
    )
  }

  /**
   * 获取两点之间的方向向量
   */
  static getDirectionVector(pointA, pointB, normalize = true) {
    const direction = pointB.clone().sub(pointA)
    return normalize ? direction.normalize() : direction
  }

  /**
   * 计算中点
   */
  static getMidpoint(pointA, pointB) {
    return pointA.clone().add(pointB).multiplyScalar(0.5)
  }
}
